﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net;
using System.Data;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
public partial class connectInfo : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

        String ss = getContent("http://www.mafengwo.cn/mdd/");
        string[] prstr = Regex.Split(ss, "<h2>目的地导航：</h2></div>");
        ss = prstr[1];
        string[] prstr1 = Regex.Split(ss, "<div class=\"clr3\"></div>");
        ss = prstr1[0];
        ss = ss.Replace("<div class=\"mdh_li\">","");
        ss = ss.Replace("<ul>", "");
        ss = ss.Replace("</ul>", "");
        ss = ss.Replace("<li><a href=\"", "http://mafengwo.cn");
        ss = ss.Replace("</a> </li>", "");
        ss = ss.Replace(" ", "");
        
        string[] pristr = Regex.Split(ss,"\r\n");
        for (int i = 0; i < pristr.Length; i++)
        {
            string[] priurl = Regex.Split(pristr[i], "\">");

            if (!priurl[0].Equals(""))
            {
                String strcity = getContent(priurl[0]);
                DataSet rds = new Wozuji.BLL.region_t().GetList(" replace(region_code,' ','') like'%0000'and region_name like'" + priurl[1] + "%'");
                string regioncode = "999999";
                if (rds.Tables[0].Rows.Count > 0)
                {
                     regioncode = rds.Tables[0].Rows[0]["region_code"].ToString();
                }
                
                String[] ci1 = Regex.Split(strcity, "id=\"pnl_scroll\">", RegexOptions.IgnoreCase);
                strcity = ci1[1];
                String[] ci2 = Regex.Split(strcity, "<div class=\"btn_bot\">", RegexOptions.IgnoreCase);
                strcity = ci2[0];
                string[] ci3 = Regex.Split(strcity, "</li>", RegexOptions.IgnoreCase);
                for (int j = 0; j < ci3.Length; j++)
                {
                    Response.Write(ci3[j]+"<br>");
                    string[] cd1 = Regex.Split(ci3[j], "<a href=\"");

                    if (cd1.Length < 2) { continue; }
                    else
                    {
                        //得到热度
                        string rd = ci3[j].Substring(ci3[j].IndexOf("热度:"), ci3[j].Length - ci3[j].IndexOf("热度:"));
                        rd = rd.Substring(0, rd.IndexOf("</div>"));
                        string cdurl = cd1[1].Substring(0, cd1[1].IndexOf("\"><img"));
                        string img;
                        if (cd1[1].IndexOf("jpeg\"") > 0)
                        {
                            img = cd1[1].Substring(cd1[1].IndexOf("<img src=\"")+10, cd1[1].IndexOf("jpeg\"") - cd1[1].IndexOf("<img src=\"") + 4-10);
                        }
                        else
                        {
                            img = "";
                        }
                        string cname = cd1[2].Substring(cd1[2].IndexOf("html\">")+6, cd1[2].IndexOf("</a>") - cd1[2].IndexOf("html\">")-6);

                        //Response.Write(cdurl + "|" + img+"|"+cname+"<br>");
                        //得到详细页面内容
                        string dtlstr = getContent("http://mafengwo.cn"+cdurl);

                        String[] tgl = Regex.Split(dtlstr, "<div class=\"tag_list\">", RegexOptions.IgnoreCase);


                        string taglist = tgl[1].Substring(0, tgl[1].IndexOf("</div>"));
                        string imageurl = "";
                        int a = dtlstr.IndexOf(") no-repeat;\">");
                        int c = dtlstr.IndexOf("<div class=\"city_hd \" style=\"background:url(");
                        if (dtlstr.IndexOf("<div class=\"city_hd\" style=\"background:url(") <1)
                        {
                            imageurl = dtlstr.Substring(dtlstr.IndexOf("<div class=\"city_hd \" style=\"background:url(") + 44, dtlstr.IndexOf(") no-repeat;\">") - dtlstr.IndexOf("<div class=\"city_hd \" style=\"background:url(") - 44);
                        }
                        
                        else
                        {
                             imageurl = dtlstr.Substring(dtlstr.IndexOf("<div class=\"city_hd\" style=\"background:url(") + 43, dtlstr.IndexOf(") no-repeat;\">") - dtlstr.IndexOf("<div class=\"city_hd\" style=\"background:url(") - 43);
                        }
                        //Response.Write(cname + "<br>" + taglist + "<br>" + imageurl+"<br>");
                        //string content = new Wozuji.Common.HttpHelper().RemoveHTML(strcity);
                        //TextBox1.Text = content;
                        string target = "";
                        if (taglist.IndexOf("html\">") > 0)
                        { 
                            taglist=taglist.Replace("html\">","IIII");
                            taglist = taglist.Replace("</a>", "IIII");
                            string[] tagary = Regex.Split(taglist, "IIII", RegexOptions.IgnoreCase);
                            for (int t = 1; t < tagary.Length; t = t + 2)
                            {
                                target = tagary[t] + "|" + target;
                            }
                            //Response.Write(target);
                            Wozuji.Model.Destination_t dst = new Wozuji.Model.Destination_t();
                            dst.Name = cname;
                            dst.region = regioncode;
                            dst.mailpic = imageurl;
                            dst.meili = target;
                            dst.Dstdesc = priurl[1]+"|"+cdurl;
                            dst.degree = Convert.ToInt32(rd.Replace("热度:", ""));
                            
                            new Wozuji.BLL.Destination_t().Add(dst);
                            
                            
                        }
                    }
                }
                    
            }
            else
            {
                continue;
            }

           
        }


        //String a1 = ss.Substring(ss.IndexOf("<strong>都市</strong>"));
        //a1 = a1.Substring(0, a1.IndexOf("<dt class=\"tit3\">"));
        //a1 = a1.Replace("<p>","");
        //a1 = a1.Replace("</p>", "");
        //a1 = a1.Replace("<li>","");
        //a1 = a1.Replace("</li>", "");
        //a1 = a1.Replace("</strong>", "");
        //a1 = a1.Replace("</div>", "");
        //a1 = a1.Replace("<dl>", "");
        //a1 = a1.Replace("</dl>", "");
        //a1 = a1.Replace("<dd>", "");
        //a1 = a1.Replace("</dd>", "");
        //a1 = a1.Replace("</ul>", "");
        //a1 = a1.Replace("<div class=\"extra\"><a href=\"/mdd/\">其它景点&gt;","");
        //string[] a2 = Regex.Split(a1, "<strong>", RegexOptions.IgnoreCase);
        //foreach (string i in a2)
        //{
            
        //    string[] b1 = Regex.Split(i.ToString(), "</a>", RegexOptions.IgnoreCase);
        //    for (int j=0;j<b1.Length;j++)
        //    {
        //        if (j == 0)
        //        {
        //            if (b1[j].IndexOf("mdd") > 0)
        //            {
        //                string pstr = b1[j].Replace("<a href=\"", "");
        //                string[] psplit = Regex.Split(pstr.ToString(), "\">", RegexOptions.IgnoreCase);

        //                //Response.Write(psplit[0] + "<br>"+psplit[1]);
        //                 //DataSet ds = new Wozuji.BLL.region_t().GetList(" replace(region_code,' ','') like'%0000'and region_name like'"+psplit[1]+"%'");
        //                //由省进入市景点
        //                 String strcity = getContent("http://mafengwo.cn" + psplit[0]);
        //                String[] ci1=Regex.Split(strcity,"id=\"pnl_scroll\">",RegexOptions.IgnoreCase);
        //                strcity = ci1[1];
        //                String[] ci2 = Regex.Split(strcity, "<div class=\"btn_bot\">", RegexOptions.IgnoreCase);
        //                strcity = ci2[0];
        //                Response.Write(strcity);


        //            }
                   
                    
        //        }
        //        //b1[j]=b1[j].Replace("<a href=", "");
        //       // string[] cc = Regex.Split(b1[j], "\"", RegexOptions.IgnoreCase);
        //       // string c1 = b1[j].Substring(b1[j].IndexOf('"', 0, 1), b1[j].LastIndexOf('"') - b1[j].IndexOf('"', 0, 0));
                
        //    }

        //}
        //TextBox1.Text = ss;






    }
    public string getContent(string Url)
    {
        string strResult = "";
        try
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);
            //声明一个HttpWebRequest请求  
            request.Timeout = 30000;
            request.KeepAlive = false;
            request.UserAgent = "Mozilla/4.0   (compatible;   MSIE   6.0;   Windows   NT   5.2;   .NET   CLR   1.1.4322)";
            request.ProtocolVersion = HttpVersion.Version10;
            //设置连接超时时间  
            request.Headers.Set("Pragma", "no-cache");
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            Stream streamReceive = response.GetResponseStream();
            Encoding encoding = Encoding.GetEncoding("UTF-8");
            StreamReader streamReader = new StreamReader(streamReceive, encoding);
            strResult = streamReader.ReadToEnd();
            streamReader.Close();
        }
        catch
        {
            //throw;
            return "";
        }
        return strResult;
    } 
}

